// src/stores/user.js
import { defineStore } from 'pinia'
import { login } from '@/api/user'
import { setToken, removeToken } from '@/utils/auth'
import { menuList } from '@/api/dataTest.js'
import { usePermissionStore } from '@/stores/permission.js'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    userInfo: {},
    menus: [] // 用户权限菜单
  }),
  actions: {
    async login(userInfo) {
      // 登录获取 token
      const res = await login(userInfo)
      this.token = res.data.token

      // 获取用户菜单权限
      // const menuRes = await getMenuList()
      // const menuRes = menuList
      // // 添加404页面（必须放在最后）
      // menuRes.push({
      //   path: '/:pathMatch(.*)*',
      //   redirect: '/404',
      //   hidden: true
      // });
      //
      // // 生成动态路由
      // const permissionStore = usePermissionStore()
      // await permissionStore.generateRoutes(menuRes)

      return res
    },

  async getUserInfo() {
    // 调用 API 获取用户信息和菜单
    const response = await getInfo()
    this.userInfo = response.data.user
    this.menus = response.data.menus
    return response
  },

  logout() {
    this.token = ''
    this.userInfo = {}
    this.menus = []
    removeToken()
    // 跳转到登录页的逻辑可以在这里或路由守卫中处理
  }
}
})
